package com.baosight.sczx.zm.cb.service; 
import java.util.ArrayList;
import java.util.HashMap; 
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cb.Coalseam;
import com.baosight.sczx.common.zm.cInterface.cb.RoadWay; 
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCS; 
import com.baosight.sczx.common.zm.domain.InitialDatas;
import com.baosight.sczx.zm.cb.domain.ZMCBRoadway; 
import com.baosight.sczx.zm.cb.domain.ZMCBWorkingface;

/**
 * <p>
 * Title: ServiceZMCB05.java
 * </p> 
 */
public class ServiceZMCB05 extends ServiceEPBase {
	private String fdParentId = "";

	/**
	 * 初始进入方法 
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	/**
	 * 查询方法 
	 */
	public EiInfo query(EiInfo inInfo) {
		fdParentId = inInfo.getString("fdMineId");
		String[] _nodeId=fdParentId.split("_");
		fdParentId=_nodeId[0];
		StaticTools.setQueryParameters(inInfo, "fdParentId", fdParentId, true);//添加查询条件 
		EiInfo outInfo = super.query(inInfo, "ZMCBRoadway.query", new ZMCBRoadway());
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.roadwayType", false));// 查询区'巷道类型'
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.mineRock", false));// 煤岩别
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.supportingType", true));// 支护形式
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.roadwayUsing", false));// 巷道用途
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.grubProcess", false));// 掘进工艺
		outInfo.set("roadwayNoType", SystemSettingCS.getInstance().getRoadwayNo());// 巷道编号形式 0自动 1手动 2生成
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.sectionShape", true));// 断面形状
		outInfo.addBlock(RoadWay.getParnetInfoRoadWayBlock());// 巷道所属下拉框
		outInfo.addBlock(Coalseam.getWorkStatusBlock("cmes.cb.status"));// 施工状态
		outInfo.addBlock(RoadWay.getParnetInfoRoadWayBlock());
		return outInfo;
	}

	/**
	 * 新增方法 
	 */

	public EiInfo insert(EiInfo inInfo) {
		String thisfdParentId = "";
		EiInfo out=new EiInfo(); 
		ZMCBRoadway _dom = new ZMCBRoadway();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
//			String numberType = SystemSettingCS.getInstance().getRoadwayNo(); // 编号形式0自动
// 1手动
// 2生成
//			if (numberType != null && numberType.equals("0")) {
//				SystemSettingCSNumber _cs = new SystemSettingCSNumber("2");
//				String fdEngineeringdesignId = _dom.getFdEngineeringdesignId();
//				String _temp = _cs.generateNewNumber(fdEngineeringdesignId);
//				_dom.setFdNo(_temp);//编号
//			}
			_dom.setFdAbbreviation(_dom.getFdName());
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CI_ROADWAY"));//主键
			thisfdParentId=_dom.getFdParentId();
			_dom.setFdDeleteStatus("0");
			_dom.setFdSlope((double)0);
			_dom.setFdRemarks(UserSession.getInSessionProperty("miningCode").toString());
			_dom.setFdCompleteLength((double)0);
			this.dao.insert("ZMCBRoadway.insert",_dom);
			 
		}
		inInfo.set("fdMineId", thisfdParentId);
		inInfo.set("fdParentId", thisfdParentId);
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
	}

	/**
	 * 修改方法 
	 */

	public EiInfo update(EiInfo inInfo) {
		String thisfdParentId = "";
		EiInfo out=new EiInfo(); 
		ZMCBRoadway _dom = new ZMCBRoadway();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId",_dom.getFdKeyId());
			thisfdParentId=_dom.getFdParentId();
			this.dao.update("ZMCBRoadway.update",_dom);
		}
		inInfo.set("fdMineId", thisfdParentId);
		inInfo.set("fdParentId", thisfdParentId);
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
	}
	/**
	 * 删除方法 
	 */
	public EiInfo delete(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCBRoadway _dom = new ZMCBRoadway();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("ZMCBRoadway.delete", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	}

	/**
	 * 校验名称编号不能重复 
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate(EiInfo inInfo) {
		Map<String, String> params = new HashMap<String, String>();
		ZMCBRoadway _dom = new ZMCBRoadway();
		String oper=(String)inInfo.get("efCurButtonEname");
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			String fdNo = _dom.getFdNo();
			String fdName = _dom.getFdName();
			String fdAbbreviation = _dom.getFdAbbreviation();
			params.put("fdNo", fdNo);
			params.put("fdName", fdName);
			params.put("fdAbbreviation", fdAbbreviation);
			if(oper.equalsIgnoreCase("update1")||oper.equalsIgnoreCase("update"))
			{
				params.put("fdKeyId", _dom.getFdKeyId());
			}
			List result = dao.query("ZMCBRoadway.validate", params);
			if (result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				ZMCBRoadway _temp = (ZMCBRoadway) result.get(0);
				StringBuffer msg = new StringBuffer("提交失败，");
				if (_temp.getFdNo() != null && _temp.getFdNo().equals(fdNo)) {
					msg.append(" 巷道编号 ");
				}
				if (_temp.getFdName() != null && _temp.getFdName().equals(fdName)) {
					msg.append(" 巷道名称 ");
				}
	/*			if (_temp.getFdAbbreviation() != null
						&& _temp.getFdAbbreviation().equals(fdAbbreviation)) {
					msg.append(" 简称 ");
				}*/
				msg.append("重复！");
				inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}
}
